package com.sc.nft.dao;

import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sc.nft.entity.ActivityBasics;
import com.sc.nft.entity.dto.ActivationRecordDTO;
import com.sc.nft.entity.dto.ActivityBasicsPageDTO;
import com.sc.nft.entity.vo.*;
import com.sc.nft.enums.AdminActivityTypeEnum;
import com.sc.nft.enums.PrizeDrawActivityTypeEnum;
import com.sc.nft.mapper.ActivityBasicsMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

/**
 * (ActivityBasics)表Dao层
 *
 * @author 王伟成
 * @since 2023-09-12 16:33:45
 */
@Slf4j
@Repository
public class ActivityBasicsDao extends ServiceImpl<ActivityBasicsMapper, ActivityBasics> {



    public Boolean lottery(String lotteryDrawTime, Long id,String lotteryDrawResult){
        LambdaUpdateWrapper<ActivityBasics>lambdaUpdateWrapper= Wrappers.lambdaUpdate();
        lambdaUpdateWrapper.eq(ActivityBasics::getLotteryTimeVar,lotteryDrawTime);
        lambdaUpdateWrapper.set(ActivityBasics::getLotteryResultId,id);
        lambdaUpdateWrapper.set(ActivityBasics::getLotteryStatus,1);
        lambdaUpdateWrapper.set(ActivityBasics::getLotteryDrawResult,lotteryDrawResult);
        return update(lambdaUpdateWrapper);
    }
    public Page<ActivityBasicsVO> activityBasics(ActivityBasicsPageDTO activityListDTO) {
        return baseMapper.activityBasics(activityListDTO.toMp(),activityListDTO.getType().getCode());
    }
    public void unmount(Long id, Boolean aTrue) {
        LambdaUpdateWrapper<ActivityBasics> lambdaUpdateWrapper = Wrappers.lambdaUpdate();
        lambdaUpdateWrapper.eq(ActivityBasics::getId,id);
        lambdaUpdateWrapper.set(ActivityBasics::getStatus,aTrue);
        update(lambdaUpdateWrapper);
    }
    public Page<ActivityBasics> webPage(Page page) {
        LambdaQueryWrapper<ActivityBasics> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(ActivityBasics::getStatus,1);
        lambdaQueryWrapper.orderByDesc(ActivityBasics::getSort);
        return page(page,lambdaQueryWrapper);
    }

    public Boolean getBooleanByIntraCycle(Date startTime, Date endTime, PrizeDrawActivityTypeEnum type) {
        LambdaQueryWrapper<ActivityBasics> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ActivityBasics::getType,type);
        wrapper.between(ActivityBasics::getStartTime,startTime,endTime);
        return count(wrapper) >0;
    }


    public List<ActivityBasics> getDataThatMeetsLotteryConditions(Integer type) {
        LambdaQueryWrapper<ActivityBasics> wrapper = new LambdaQueryWrapper<>();
        // 活动类型
        wrapper.eq(ActivityBasics::getType,type);
        // 没有结算的
        wrapper.eq(ActivityBasics::getIsSettle,Boolean.FALSE);
        // 发布状态的
        wrapper.eq(ActivityBasics::getStatus,1);
        // 已经开奖的
        wrapper.eq(ActivityBasics::getLotteryStatus,1);
        // 开奖时间必须小于当前时间
        wrapper.le(ActivityBasics::getLotteryTime, DateTime.now());
        return list(wrapper);
    }

    public List<ActivityBasics> getLotteryDataByType(int type) {
        return baseMapper.getLotteryDataByType(type);
    }
    public IPage<ActivityRecordVO> activityRecord(Page page) {
        return baseMapper.activityRecord(page);
    }

    public MyNumberVO myNumber(Long id) {
        return baseMapper.myNumber(id);
    }
    public IPage<ActivationRecordVO> activationRecord(ActivationRecordDTO activationRecordDTO) {
        return baseMapper.activationRecord(activationRecordDTO.toMp(),activationRecordDTO);
    }
    public void sort(Long id, Integer sort) {
        LambdaUpdateWrapper<ActivityBasics> lambdaUpdateWrapper = Wrappers.lambdaUpdate();
        lambdaUpdateWrapper.eq(ActivityBasics::getId,id);
        lambdaUpdateWrapper.set(ActivityBasics::getSort,sort);
        update(lambdaUpdateWrapper);
    }

    public Boolean updateLotteryStatusAndIsSettleById(Long id, int lotteryStatus,Boolean isSettle) {
        LambdaUpdateWrapper<ActivityBasics> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(ActivityBasics::getId,id);
        wrapper.set(ActivityBasics::getLotteryStatus,lotteryStatus);
        wrapper.set(ActivityBasics::getIsSettle,isSettle);
        return update(wrapper);
    }

    /**
     * 读取当前所上架的活动列表
     * @return
     */
    public List<ActivityBasicsListVO> activityBasicsList() {
        return baseMapper.activityBasicsList();
    }

}
